/**    
* @Title: GroupServiceImpl.java  
* @Package com.kaver.sso.service.impl   
* @author roc   
* @date Aug 9, 2021 10:10:12 AM  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.sso.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaver.sso.dao.po.SsoGroup;
import com.kaver.sso.dao.service.SsoGroupDao;
import com.kaver.sso.dao.service.SsoRightsDao;
import com.kaver.sso.dao.service.SsoRoleDao;
import com.kaver.sso.def.BizException;
import com.kaver.sso.def.dto.GoupDto;
import com.kaver.sso.def.enums.CodeEnum;
import com.kaver.sso.def.req.AddGroupReq;
import com.kaver.sso.def.req.DelGroupReq;
import com.kaver.sso.def.req.GroupListReq;
import com.kaver.sso.def.req.GroupPageListReq;
import com.kaver.sso.def.req.UpdateGroupReq;
import com.kaver.sso.def.resp.PageResp;
import com.kaver.sso.service.GroupService;

/**  
* @ClassName: GroupServiceImpl  
* @Description: 权限组
* @author roc  
* @date Aug 9, 2021 10:10:12 AM  
*    
*/
@Service
public class GroupServiceImpl implements GroupService{
    @Autowired
    SsoGroupDao ssoGroupDao;
    @Autowired
    SsoRoleDao ssoRoleDao;
    @Autowired
    SsoRightsDao ssoRightsDao;
    @Override
    public void addGroup(AddGroupReq req) {
        SsoGroup entity = new SsoGroup();
        entity.setGroupName(req.getGroupName());
        ssoGroupDao.save(entity);
    }

    @Override
    public void updateGroup(UpdateGroupReq req) {
        SsoGroup group = ssoGroupDao.getById(req.getGroupId());
        if (group == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        group.setGroupName(req.getGroupName());
        group.updateById();
    }

    @Override
    public void delGroup(DelGroupReq req) {
        SsoGroup group = ssoGroupDao.getById(req.getGroupId());
        if (group == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        int count = ssoRoleDao.queryGroupRoleCount(group.getId());
        if (count > 0) {
        	throw new BizException(CodeEnum.F_000001.getCode(), "当前权限组绑定了角色，请先清除角色");
        }
        int cot =ssoRightsDao.queryGroupRightCount(group.getId());
        if (cot > 0) {
        	throw new BizException(CodeEnum.F_000001.getCode(), "当前权限组绑定了权限，请先清除权限");
        }
        group.deleteById();
    }

    @Override
    public PageResp<GoupDto> groupPageList(GroupPageListReq req) {
        QueryWrapper<SsoGroup> queryWrapper = new QueryWrapper<SsoGroup>();
        if (req.getGroupName() != null) {
            queryWrapper.like("group_name", req.getGroupName());
        }
        queryWrapper.orderByDesc("id");
        IPage<SsoGroup> page = new Page<>(req.getPage(), req.getSize());
        IPage<SsoGroup> iPage = ssoGroupDao.page(page, queryWrapper);
        PageResp<GoupDto> pageResp = new PageResp<>();
        List<GoupDto> lists = new ArrayList<>();
        List<SsoGroup> groups = iPage.getRecords();
        if (!CollectionUtils.isEmpty(groups)) {
            for (SsoGroup group : groups) {
                GoupDto dto = new GoupDto();
                dto.setGroupId(group.getId());
                dto.setGroupName(group.getGroupName());
                lists.add(dto);
            }
        }
        pageResp.setLists(lists);
        pageResp.setTotal(iPage.getTotal());
        return pageResp;
    }

    @Override
    public List<GoupDto> groupList(GroupListReq req) {
        QueryWrapper<SsoGroup> queryWrapper = new QueryWrapper<SsoGroup>();
        if (req.getGroupName() != null) {
            queryWrapper.like("group_name", req.getGroupName());
        }
        queryWrapper.orderByDesc("id");
        List<SsoGroup> groups = ssoGroupDao.list(queryWrapper);
        List<GoupDto> lists = new ArrayList<>();
        if (!CollectionUtils.isEmpty(groups)) {
            for (SsoGroup group : groups) {
                GoupDto dto = new GoupDto();
                dto.setGroupId(group.getId());
                dto.setGroupName(group.getGroupName());
                lists.add(dto);
            }
        }
        return lists;
    }


}
